<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
{
    protected $fillable = ['dwmc', 'dwdm', 'type', 'file_type', 'order', 'status', 'fail_reason', 'created_at', 'updated_at'];
    const FILE_TYPE_BZ_SHUZHI = 1;
    const FILE_TYPE_BZ_KHB = 2;
    const FILE_TYPE_DJ_SHUZHI = 3;
    const FILE_TYPE_DJ_GZZJ = 4;

    const TYPE_BZ = 1;
    const TYPE_DJ = 2;

    const STATUS_NORMAL = 1;
    const STATUS_VERIFY_PENDING = 2;
    const STATUS_VERIFY_FAILED = 3;
    const STATUS_VERIFY_SUCCESS= 4;

    public static $statusMaps = [
        self::STATUS_NORMAL => '未提交',
        self::STATUS_VERIFY_PENDING => '待审核',
        self::STATUS_VERIFY_SUCCESS => '已通过',
        self::STATUS_VERIFY_FAILED => '未通过'
    ];
    public static $statusColorMaps = [
        self::STATUS_NORMAL => '#4277cf',
        self::STATUS_VERIFY_PENDING => '#dda451',
        self::STATUS_VERIFY_SUCCESS => '#21b978',
        self::STATUS_VERIFY_FAILED => '#ea5455'
    ];

    public static $verifyStatusForm = [
        self::STATUS_VERIFY_SUCCESS => '通过',
        self::STATUS_VERIFY_FAILED => '未通过'
    ];

    public static function findByDwdm($dwdm)
    {
        return self::where('dwdm', $dwdm)->first();
    }

    public function getFileShouldName()
    {
        if (in_array($this->file_type, [Department::FILE_TYPE_BZ_SHUZHI, Department::FILE_TYPE_BZ_KHB])) {
            return $this->getBzFileShouleName();
        }
        if (in_array($this->file_type, [Department::FILE_TYPE_DJ_SHUZHI, Department::FILE_TYPE_DJ_GZZJ])) {
            return $this->getDjFileShouldName();
        }
        return '';
    }

    /**
     * 获取班子考核应该的文件名称
     */
    public function getBzFileShouleName()
    {
        $should_origin_name = '';
        if ($this->file_type == Department::FILE_TYPE_BZ_SHUZHI) {
            $should_origin_name = $this->dwmc . "述职PPT";
        } else  if ($this->file_type == Department::FILE_TYPE_BZ_KHB){
            $should_origin_name = $this->dwmc . "班子考核表";
        }
        return $should_origin_name;
    }

    /**
     * 获取党建的应该的文件名称
     */
    public function getDjFileShouldName()
    {
        $should_origin_name = '';
        if ($this->file_type == Department::FILE_TYPE_DJ_SHUZHI) {
            $should_origin_name = $this->dwmc . "述职PPT";
        } else  if ($this->file_type == Department::FILE_TYPE_DJ_GZZJ){
            $should_origin_name = $this->dwmc . "党建工作总结";
        }
        return $should_origin_name;
    }

    public function getDirPrefix()
    {
        if (in_array($this->file_type, [Department::FILE_TYPE_BZ_SHUZHI, Department::FILE_TYPE_BZ_KHB])) {
            return "班子考核";
        }
        if (in_array($this->file_type, [Department::FILE_TYPE_DJ_SHUZHI, Department::FILE_TYPE_DJ_GZZJ])) {
            return "党建考核";
        }
        return 'error';
    }

    public function file()
    {
        return $this->hasOne(DepartmentFile::class, 'dwdm', 'dwdm');
    }

    public function staffs()
    {
        return $this->hasMany(DepartmentStaff::class, 'dwdm', 'dwdm');
    }

    public function fushus()
    {
        return $this->hasMany(DepartmentFushu::class, 'dwdm', 'dwdm');
    }

    public function checkAllFilesReady()
    {
        $department_type = $this->type;
        $department_file = DepartmentFile::query()->where('dwdm', $this->dwdm)->first();

        # 判断所有材料是否都已上传完成
        $errors = [];
        if ($department_type == Department::TYPE_BZ) {
            if ($this->file_type == Department::FILE_TYPE_BZ_SHUZHI) {
                if (!$department_file->ppt_filepath || ($department_file->ppt_filepath && !file_exists(file_local_path($department_file->ppt_filepath)))) {
                    array_push($errors, '请上传行政述职PPT');
                }
                if (!$department_file->pdf_filepath || ($department_file->pdf_filepath && !file_exists(file_local_path($department_file->pdf_filepath)))) {
                    array_push($errors, '请上传行政述职PPT转PDF版本');
                }
            } else if ($this->file_type == Department::FILE_TYPE_BZ_KHB) {
                if (!$department_file->pdf_filepath || ($department_file->pdf_filepath && !file_exists(file_local_path($department_file->pdf_filepath)))) {
                    array_push($errors, '请上传行政班子考核表PDF版本');
                }
            } else {
                array_push($errors, '发生错误');
            }

            # 判断附属单位材料是否已经上传完成
            $fushus = DepartmentFushu::getFushuByDwdm($this->dwdm);
            foreach($fushus as $fushu) {
                if (!$fushu->filepath || ($fushu->filepath && !file_exists(file_local_path($fushu->filepath)))) {
                    array_push($errors, "请上传{$fushu->fs_dwmc}班子考核表");
                }
            }
            # 判断个人材料是否已经上传完成
            $personals = DepartmentStaff::getStaffsByDwdm($this->dwdm);
            foreach($personals as $personal) {
                if (!$personal->filepath || ($personal->filepath && !file_exists(file_local_path($personal->filepath)))) {
                    array_push($errors, "请上传{$personal->name}考核表");
                }
            }
        } else if ($department_type == Department::TYPE_DJ) {
            if ($this->file_type == Department::FILE_TYPE_DJ_SHUZHI) {
                if (!$department_file->ppt_filepath || ($department_file->ppt_filepath && !file_exists(file_local_path($department_file->ppt_filepath)))) {
                    array_push($errors, '党建述职PPT');
                }
                if (!$department_file->pdf_filepath || ($department_file->pdf_filepath && !file_exists(file_local_path($department_file->pdf_filepath)))) {
                    array_push($errors, '请上传党建述职PPT转PDF版本');
                }
            } else if ($this->file_type == Department::FILE_TYPE_BZ_KHB) {
                if (!$department_file->pdf_filepath || ($department_file->pdf_filepath && !file_exists(file_local_path($department_file->pdf_filepath)))) {
                    array_push($errors, '请上传党建工作总结PDF版本');
                }
            } else {
                array_push($errors, '发生错误');
            }
        } else {
            array_push($errors, '发生错误');
        }
        return $errors;
    }

    public function getStatusText()
    {
        $text = Department::$statusMaps[$this->status];
        $color = Department::$statusColorMaps[$this->status];
        $html = "<span style='color: ".$color."'><strong>".$text."</strong></span>";
        return $html;
    }

    public function getStatusTextForCardHeader()
    {
        $html = "";
        if (in_array($this->status, [Department::STATUS_VERIFY_PENDING, Department::STATUS_VERIFY_FAILED, Department::STATUS_VERIFY_SUCCESS])) {
            $html .= $this->getStatusText();
            if ($this->status == Department::STATUS_VERIFY_FAILED && $this->fail_reason) {
                $html .=  "<br/>原因：{$this->fail_reason}";
            }
        }
        return $html;
    }

    public static function getStatusForSelector($type)
    {
        $options = [];
        foreach(self::$statusMaps as $status => $status_text) {
            $count = Department::query()->where('type', $type)->where('status', $status)->count();
            $options[$status] = $status_text . "[{$count}]";
        }
        return $options;
    }

    public static function generateDwdm()
    {
        do {
            $dwdm = randomIntString(3);
        } while (Department::query()->where('dwdm', $dwdm)->exists());
        return $dwdm;
    }
}